layui.use(['form', 'layer', 'upload'], function () {
    const form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        $ = layui.jquery;

    //封面图上传
    let coverUrl = null;
    const uploadInst = upload.render({
        elem: '#test1'
        , url: host + '/a03/umsmember/uploadImage'
        , method: 'post'  //可选项。HTTP类型，默认post
        , field: 'multipartFile'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code !== 200) {
                return layer.msg('上传失败');
            } else {
                //上传成功
                coverUrl = res.data;
            }
        }
        , error: function () {
            //演示失败状态，并实现重传
            const demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });

    $.ajax({
        url: $.cookie("tempUrl") + "hSysDoctor/selectByPrimaryKey?id=" + $.cookie("doctorId"),
        type: "GET",
        success: function (result) {
            if (result.code === 200) {
                // console.log(result.data);
                $(".id").val(result.data.id);
                $(".username").val(result.data.nickname);
                $(".phone").val(result.data.phone);
                if (result.data.sex === 1) {
                    $(".nan").attr("checked", true);
                } else if (result.data.sex === 2) {
                    $(".nv").attr("checked", true);
                }
                // form.render('radio');
                // var sex= $(":checked").val();
                // var sex = $('input[name="sex"]:checked').val();
                // console.log(sex);

                $(".doctorCertId").val(result.data.doctorCertId);
                $(".type").val(result.data.type === 1 ? "护士" : "医生");

                //获取所在医院
                $.ajax({
                    url: $.cookie("tempUrl") + "hGroup/selectById?id=" + result.data.belongGroupId,
                    type: "GET",
                    success: function (result) {
                        $(".hospital").val(result.data.name);
                    }
                });

                //获取医生职称
                $.ajax({
                    url: $.cookie("tempUrl") + "DoctorTitle/selectById?id=" + result.data.doctorTitleId,
                    type: "GET",
                    success: function (result) {
                        //获取自身医生职称
                        let doctorTitle = result.data.title;

                        //获取医生职称列表，并给自身赋值
                        $.ajax({
                            url: $.cookie("tempUrl") + "DoctorTitle/selectList",
                            type: "GET",
                            success: function (result) {
                                let strArray = result.data;
                                // console.log(strArray)
                                for (let index in strArray) {
                                    if (strArray[index].title === doctorTitle) {
                                        $(".doctorTitleId").append("<option selected value=" + strArray[index].id + ">" + strArray[index].title + "</option>")
                                    } else {
                                        $(".doctorTitleId").append("<option value=" + strArray[index].id + ">" + strArray[index].title + "</option>")
                                    }
                                }
                                form.render();
                            }
                        });
                    }
                });

                //获取所属科室
                $.ajax({
                    url: $.cookie("tempUrl") + "hDepartment/selectById?id=" + result.data.deptId,
                    type: "GET",
                    success: function (result) {
                        //获取自身所属科室
                        let doctorDepartment = result.data.name;
                        //获取所属科室列表，并给自身赋值
                        $.ajax({
                            url: $.cookie("tempUrl") + "hDepartment/selectListNotPage",
                            type: "GET",
                            success: function (result) {
                                let strArray = result.data;
                                for (let index in strArray) {
                                    if (strArray[index].name === doctorDepartment) {
                                        $(".deptId").append("<option selected value=" + strArray[index].id + ">" + strArray[index].name + "</option>")
                                    } else {
                                        $(".deptId").append("<option value=" + strArray[index].id + ">" + strArray[index].name + "</option>")
                                    }
                                }
                                form.render();
                            }
                        });
                    }
                });

                $(".icon").attr("src", result.data.icon);  //头像

                form.render();
            } else {
                layer.msg(result.exception, {icon: 7, anim: 6});
            }
        }
    });


    form.on("submit(submit)", function (data) {
        layer.confirm('修改信息将会被重新审核，是否确认?', {title: '系统提示', area: ['320px', '200px']}, function (index0) {
            //do something
            //弹出loading
            const index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
            $.ajax({
                url: $.cookie("tempUrl") + "hSysDoctor/updateByPrimaryKeySelective",
                type: "PUT",
                datatype: "application/json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    id: $.cookie("doctorId"),
                    // icon: coverUrl,
                    icon: coverUrl,
                    nickname: data.field.username,
                    sex: data.field.sex,
                    phone: data.field.phone,
                    doctorTitleId: data.field.doctorTitleId,
                    deptId: data.field.deptId
                }),
                success: function (result) {
                    if (result.code === 200) {
                        layer.msg("更新成功");
                        $.cookie('icon', coverUrl, {path: '/'});
                        setTimeout(function () {
                            top.layer.close(index);
                            // top.layer.msg("新增成功2");
                            layer.closeAll("iframe");
                            //刷新本页面
                            location.reload();
                        }, 500);
                    } else {
                        layer.msg('更新异常', {icon: 7, anim: 6});
                    }
                }
            });

            layer.close(index);
        });
        return false;
    });
});